//
//  YsCloudFileDownloadCell.m
//  YsCloudFileManageSDK
//
//  Created by 宋宏康 on 2020/3/2.
//

#import "YsCloudFileUploadCell.h"
#import "YsCloudFileManageSDKHelper.h"
#import "NSString+YscCloudFileTools.h"
#import "YsCloudFileDownloadButton.h"
#import <Masonry/Masonry.h>
#import <YYKit/YYKit.h>
#import <DownloadButton/PKDownloadButton.h>
#import <ReactiveObjC/ReactiveObjC.h>

@interface YsCloudFileUploadCell()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *timeAndSizeLabel;
@property(nonatomic,retain) YsCloudFileDownloadButton *downloadButton;
@end


@implementation YsCloudFileUploadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)initSubviews
{
    [super initSubviews];
        
    UIImageView *leftImageView = [[UIImageView alloc] init];
    self.leftImageView = leftImageView;
    [self.contentView addSubview:leftImageView];
    
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(22);
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"测试数据";
    self.nameLabel = nameLabel;
    [self.contentView addSubview:nameLabel];

    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(15);
       make.left.mas_equalTo(leftImageView.mas_right).offset(10);
   }];
    
    
    UILabel *timeAndSizeLabel = [[UILabel alloc] init];
    timeAndSizeLabel.text = @"2020-03-02 22:03:39";
    timeAndSizeLabel.font = [UIFont systemFontOfSize:13];
    timeAndSizeLabel.textColor = [UIColor colorWithHexString:@"a0aebf"];
    self.timeAndSizeLabel = timeAndSizeLabel;
    [self.contentView addSubview:timeAndSizeLabel];

    [timeAndSizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(nameLabel.mas_left);
    }];

    YsCloudFileDownloadButton *downloadButton = [[YsCloudFileDownloadButton alloc] init];
    self.downloadButton = downloadButton;
//    downloadButton.stopButton.selected = YES;
    [self.contentView addSubview:downloadButton];
    [downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(80, 67));
    }];
}

- (void)initClicks
{
    [super initClicks];
    @weakify(self);
    /// 按钮点击事件
    [[self.downloadButton.stopButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIButton * _Nullable x) {
        @strongify(self);
        x.selected = !x.selected;
        if (self.btnClickBlock) {
            self.btnClickBlock();
        }
    }];
    
    /// 执行了setuploadModel才执行下面的blcok
//    [[self rac_signalForSelector:@selector(setuploadModel:)] subscribeNext:^(RACTuple * _Nullable x) {
//        @strongify(self);
//        /// kvo监听，忽略progress =0的值
//        /// takeUntil:self.rac_prepareForReuseSignal cell每次复用都会销毁观察者
//        [[[RACObserve(self.uploadModel, progress) ignore:@0] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
//            @strongify(self);
//            self.downloadButton.progress = [x floatValue];
//            NSLog(@"songhongkang-----%@",self.uploadModel.customCacheName);
//        }];
//    }];
}


- (void)setUploadModel:(YsDocUploadModel *)uploadModel
{
    _uploadModel = uploadModel;
    
    if (_uploadModel.fileName.isNotBlank) {
        self.nameLabel.text = _uploadModel.fileName;
    }
    if (_uploadModel.lastPathComponent.isNotBlank) {
        self.leftImageView.image = [NSString getImageWithlastPathComponent:uploadModel.lastPathComponent];
    }
    @weakify(self);
    _uploadModel.progressBlock = ^(CGFloat uploadPersent, NSString *progressLableText) {
        @strongify(self);
        self.downloadButton.progress = uploadPersent;
        NSLog(@"%f\n%@",uploadPersent,progressLableText);
    };
    self.downloadButton.progress = uploadModel.uploadPercent;
    NSString *timeAndSizeStr = nil;
    self.downloadButton.hidden = NO;
    if (uploadModel.uploadStatus == 1) { //未上传
        timeAndSizeStr = @"未上传";
        self.downloadButton.stopButton.selected = YES;
    }else if (uploadModel.uploadStatus == 2){ //2.等待中
        timeAndSizeStr = @"等待中";
        self.downloadButton.stopButton.selected = YES;
    }else if (uploadModel.uploadStatus == 3){ //正在上传
        NSString *timerstr = [NSString getDateStringWithTime:uploadModel.createTime];
        NSString *totalSize = [NSString fileSizeStringFromBytes:uploadModel.totalSize];
        timeAndSizeStr = [NSString stringWithFormat:@"%@  %@",timerstr,totalSize];
        self.downloadButton.stopButton.selected = NO;
    }else if (uploadModel.uploadStatus == 4){ //上传完成
       timeAndSizeStr = @"上传完成";
        self.downloadButton.hidden = YES;
    }else if (uploadModel.uploadStatus == 5){ //暂停
        timeAndSizeStr = @"暂停上传";
        self.downloadButton.stopButton.selected = YES;
    }else if (uploadModel.uploadStatus == 6){ //上传失败
       timeAndSizeStr = @"上传失败";
        self.downloadButton.stopButton.selected = YES;
    }else{
       NSLog(@"----------上传任务的状态未知---------");
        timeAndSizeStr = @"未知状态";
    }
    self.timeAndSizeLabel.text = timeAndSizeStr;
}

@end
